package com.easyboot.web.system.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easyboot.enums.DataStatusEnum;
import com.easyboot.framework.domain.ResultAdaptDto;
import com.easyboot.framework.domain.ZtreeDto;
import com.easyboot.framework.shiro.UserOwnInfo;
import com.easyboot.web.system.domain.SysDept;
import com.easyboot.web.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/25 15:51
 **/

@Service
public class SysDeptService {

    @Autowired
     private SysDeptMapper deptMapper;


    /**
     *
     * @author yushuo
     * @description 获取部门列表
     * @date 15:51 2021/2/25
     * @param []
     * @return java.util.List<com.easycms.web.system.domain.SysDept>
     **/
    public List<SysDept> getAllDepts(String deptName){
        LambdaQueryWrapper<SysDept> query= Wrappers.lambdaQuery();
        if(!StrUtil.isEmpty(deptName)){
            query.like(SysDept::getDeptName,deptName);
        }
        query.orderByAsc(SysDept::getOrderNum);

        List<SysDept> list= deptMapper.selectList(query);
        return list;
    }
    /**
    *
     * @author yushuo
     * @description 获取部门，树形结构
     * @date 15:51 2021/2/25
     * @param []
     * @return java.util.List<com.easycms.web.system.domain.SysDept>
    **/
    public List<SysDept> getDeptTree(){
        LambdaQueryWrapper<SysDept> query= Wrappers.lambdaQuery();
        query.orderByAsc(SysDept::getOrderNum);

        List<SysDept> list= deptMapper.selectList(query);

        List<SysDept> data=InitChild(0L,list);
        return data;
    }
    /**
    *
     * @author yushuo
     * @description 迭代子项
     * @date 16:06 2021/2/25
     * @param [pid, depts, deptId]
     * @return java.util.List<com.easycms.web.system.domain.SysDept>
    **/
    private List<SysDept> InitChild(Long pid,List<SysDept> depts){
        List<SysDept> tops=depts.stream().filter(s->s.getParentId().equals(pid)).collect(Collectors.toList());
        for (SysDept dept:tops){

            List<SysDept> subDepts=depts.stream().filter(s->s.getParentId().equals(dept.getId())).collect(Collectors.toList());
            if(subDepts.size()>0){
                List<SysDept> nextSubs=InitChild(dept.getId(),depts);
                dept.setChildren(nextSubs);
            }
        }

        return tops;
    }


    public List<ZtreeDto> getDeptZtree(){
        LambdaQueryWrapper<SysDept> query= Wrappers.lambdaQuery();
        query.orderByAsc(SysDept::getOrderNum);

        List<SysDept> list= deptMapper.selectList(query);
        List<ZtreeDto> data=new ArrayList<>();
        list.forEach(s->{
            ZtreeDto ztreeDto=new ZtreeDto();
            ztreeDto.setId(s.getId().toString());
            ztreeDto.setName(s.getDeptName());
            ztreeDto.setPId(s.getParentId().toString());
            data.add(ztreeDto);
        });

        return data;
    }


    /**
    *
     * @author yushuo
     * @description 获取实体数据
     * @date 13:44 2021/2/26
     * @param [id]
     * @return com.easycms.web.system.domain.SysDept
    **/
    public SysDept getModel(Long id){
        return deptMapper.selectById(id);
    }


    /**
    *
     * @author yushuo
     * @description 保存数据
     * @date 14:25 2021/2/26
     * @param [dept]
     * @return void
    **/
    public void save(SysDept dto, UserOwnInfo userOwnInfo){
        dto.setUpdateBy(userOwnInfo.getLoginName());
        dto.setUpdateTime(DateUtil.date());
        dto.setAncestors("");
        if(dto.getId()==0){
            dto.setCreateBy(userOwnInfo.getLoginName());
            dto.setCreateTime(DateUtil.date());
            dto.setDelFlag(DataStatusEnum.NOT_DELETED.getCode());
            deptMapper.insert(dto);
            return;
        }

        deptMapper.updateById(dto);
    }


    /**
    *
     * @author yushuo
     * @description 删除数据
     * @date 14:47 2021/2/26
     * @param [id]
     * @return void
    **/
    public ResultAdaptDto delete(Long id){
        ResultAdaptDto ret=new ResultAdaptDto();
        LambdaQueryWrapper<SysDept> query=Wrappers.lambdaQuery();
        query.eq(SysDept::getParentId,id)
                .orderByAsc(SysDept::getId)
        .last("limit 1");


        SysDept dept=deptMapper.selectOne(query);
        if(dept!=null){
            ret.setStatus(false);
            ret.setMessage("该部门下面有子部门");
            return ret;
        }
        deptMapper.deleteById(id);
        return ret;
    }
}
